// WeakMap防止内存泄漏
let map = new WeakMap()

let ob = new ResizeObserver((entries) => {
    for (const entry of entries) {
        // 运行 entry.target 对应的回调函数
        const handler = map.get(entry.target)
        if (handler) {
            handler({
                with: entry.borderBoxSize[0].inlineSize,
                height: entry.borderBoxSize[0].blockSize,
            })
        }
    }
})

export default function registerDirectives(app) {
    // 监听 echart 图标尺寸 并覆盖尺寸
    app.directive("resizeEchart", {
        mounted(el, binding) {
            // 对象映射
            map.set(el, binding.value)
            // 监听el元素尺寸变化
            ob.observe(el)
        },
        unmounted(el) {
            // 取消监听
            ob.unobserve(el)
        }
    })
}